/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.enterprise.web;

import com.assg2.dao.DaoFriend;
import com.movie.Friend;
import com.movie.FriendId;
import com.movie.Viewer;
import com.sun.org.apache.bcel.internal.generic.FDIV;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Kevin
 */
public class FriendAddCommand implements Command {

    public FriendAddCommand() {
    }

    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //TODO: set the response content type to "text/html" here
        //TODO: You'll have to use getWriter() to output some HTML texts
        //TODO: close the output stream

        response.setContentType("text/html");// from response, set content type
        PrintWriter out = response.getWriter();// from response, get output writer     
        HttpSession session = request.getSession(true);
        Viewer user = (Viewer)session.getAttribute("user");
        Viewer v = (Viewer)session.getAttribute("friend");
        String introduction = request.getParameter("introduction");
        FriendId fid = new FriendId(user.getIdviewer(), v.getIdviewer());
        Friend friend = new Friend(fid, false, introduction);
        
        DaoFriend  df = new DaoFriend();
        df.save(friend);
        
        String email = v.getEmail();
        String mail = "<b>" + user.getFirstname()+ " " + user.getLastname() + " wants to add you as a friend, you can click the link to go and check</b><a href='http://localhost:8080/assg2'>click</a>";
        
        SendMail.send(email, mail);
        
        request.setAttribute("message", "The request has been sent. <a href='viewer.jsp'>go back to homepage</a>");
        return "/message.jsp";
        
}
}